package com.qianfeng.openapi.zuul.filter;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.openapi.zuul.exception.ExceptionDict;
import com.qianfeng.openapi.zuul.feign.CacheServiceClient;
import com.qianfeng.openapi.zuul.utils.SysConstant;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/12/31
 * @Time: 下午2:20
 */
@Component
public class RouteFilter extends ZuulFilter {

    @Resource
    private CacheServiceClient cacheServiceClient;

    @Override
    public String filterType() {
        return FilterConstants.ROUTE_TYPE;//pre  post   route  error
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {

        return RequestContext.getCurrentContext().sendZuulResponse();//得到上一个过滤器的校验结果
    }

    @Override
    public Object run() throws ZuulException {
        //1.得到请求对象
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();

        //2.请求参数
        String  method = request.getParameter("method");
        //method 有没有 ？？？  redis
        //检查能否路由到指定服务
//        if ("jingdong.product.info".equals(method)){
//            //内部url：/product/info
//            //  	serviceId: product-service
//            //动态路由到
//            /**
//             *   public static final String REQUEST_URI_KEY = "requestURI";
//             *   public static final String SERVICE_ID_KEY = "serviceId";
//             */
//            context.put(FilterConstants.SERVICE_ID_KEY,"product-service");
//            context.put(FilterConstants.REQUEST_URI_KEY,"/product/info");
//
//        }
        //jingdong.product.info

        String key=SysConstant.CACHE_API_MAPPING +method;

        Map<String,String> map = cacheServiceClient.hgetall(key);
        //请求达到时间  记录日志需要计算api调用时间
        long start = System.currentTimeMillis();
        context.set("receiveTime",start);

        if(map!=null&&map.size()>0){//{}
            String serviceId = map.get("serviceId");
            String requestURL = map.get("requestURL");

            context.put(FilterConstants.SERVICE_ID_KEY,serviceId);
            context.put(FilterConstants.REQUEST_URI_KEY,requestURL);
        }else{
            context.setSendZuulResponse(false);
            context.getResponse().setContentType("application/json;charset=utf-8");
            context.setResponseStatusCode(HttpStatus.BAD_REQUEST.value());
            Map<String,Object> map1 = new HashMap<>();
            map1.put("code",ExceptionDict.NOT_FOUND);//错误码
            map1.put("msg","查无此api");//错误信息

            String json = JSON.toJSONString(map1);

            context.setResponseBody(json);
        }

        return null;
    }
}
